package C013;

/**
 * ClassName: Stack02
 * Package: C013
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/12/2 14:51
 * @Version 1.0
 * @Since 1.0
 */
public class Stack02 {
    public static void main(String[] args) {
        MyStack<Integer> stack = new MyStack<>(4);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }

    static class MyStack<T> {
        Object[] arr;
        int real, front, size, limit;

        public MyStack(int limit) {
            arr = new Object[limit];
            this.limit = limit;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public boolean isFull() {
            return size == limit;
        }

        public boolean push(T ele) {
            if(isFull()) {
                return false;
            }
            arr[front++] = ele;
            front = front == limit ? 0 : front;
            size ++;
            return true;
        }

        public T pop() {
            if(isEmpty()) {
                return null;
            }
            T ele = (T) arr[--front];
            front = front == -1 ? limit - 1 : front;
            size --;
            return ele;
        }

        public T peek() {
            if(isEmpty()) {
                return null;
            }
            return (T) arr[front - 1];
        }
    }
}
